﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace OrionBanque.Forms
{
    public partial class VirementCaC : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        private Classe.Utilisateur uA;

        public VirementCaC(Classe.Utilisateur uP)
        {
            uA = uP;
            InitializeComponent();
            ChargeComboCompte();
            RemplisCategories();
            RemplisTiers();
        }

        private void RemplisTiers()
        {
            try
            {
                List<String> ls = Classe.Operations.ChargeToutTiers(uA);
                txtTiers.DataSource = ls;
            }
            catch (Exception ex)
            {
                KryptonMessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ChargeComboCompte()
        {
            try
            {
                List<Classe.Compte> lc = Classe.Compte.ChargeTout(uA.Id);
                cbCompteOri.DisplayMember = "libelle";
                cbCompteOri.ValueMember = "id";
                cbCompteOri.DataSource = lc;

                List<Classe.Compte> lc2 = Classe.Compte.ChargeTout(uA.Id);
                cbCompteDest.DisplayMember = "libelle";
                cbCompteDest.ValueMember = "id";
                cbCompteDest.DataSource = lc2;
            }
            catch(Exception ex)
            {
                KryptonMessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void RemplisCategories()
        {
            try
            {
                List<Classe.Categorie> lc = Classe.Categorie.ChargeToutIdent();
                txtCategorie.DisplayMember = "Libelle";
                txtCategorie.ValueMember = "Id";
                txtCategorie.DataSource = lc;
            }
            catch(Exception ex)
            {
                KryptonMessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Fermer_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void OK_Click(object sender, EventArgs e)
        {
            Classe.Lien l = new Classe.Lien();
            l.TypeLien = Classe.Lien.LIEN_VIR_CAC;

            if(cbCompteOri.SelectedValue.Equals(cbCompteDest.SelectedValue))
            {
                KryptonMessageBox.Show("Veuillez sélectionner des comptes différents", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            
            Classe.Operations op1 = new OrionBanque.Classe.Operations();
            op1.Date = txtDateMvt.Value;
            op1.IdCategorie = Int32.Parse(txtCategorie.SelectedValue.ToString());
            op1.IdCompte = Int32.Parse(cbCompteOri.SelectedValue.ToString());
            op1.IdModePaiement = 4;
            op1.Libelle = txtLibelle.Text;
            op1.Montant = Double.Parse(txtMontant.Value.ToString());
            op1.Tiers = string.Empty;
            Classe.Operations.Sauve(op1);
            l.Origine = Classe.Sql.GetLastInsertId("operations");            

            op1.IdCompte = Int32.Parse(cbCompteDest.SelectedValue.ToString());
            op1.IdModePaiement = 5;
            Classe.Operations.Sauve(op1);
            l.Destination = Classe.Sql.GetLastInsertId("operations");

            Classe.Lien.Sauve(l);
            
            this.Close();
         }

        private void cbCompteOri_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblTotOri.Text = String.Format("{0,12:0,0.00}", Classe.Operations.CalculSoldOpePoint(Int32.Parse(cbCompteOri.SelectedValue.ToString()))) + " €";
        }

        private void cbCompteDest_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblTotDest.Text = String.Format("{0,12:0,0.00}", Classe.Operations.CalculSoldOpePoint(Int32.Parse(cbCompteDest.SelectedValue.ToString()))) + " €";
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            if(((MouseEventArgs)e).Button == MouseButtons.Right)
            {
                Forms.Categories fm = new Forms.Categories();
                fm.ShowDialog();
                RemplisCategories();
            }
        }
    }
}
